/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package se.kth.lwr.jeat.samples.onsitetreatment.producers;

import javax.measure.quantity.VolumetricDensity;
import javax.measure.unit.NonSI;
import javax.measure.unit.SI;
import javax.measure.unit.Unit;
import org.jscience.physics.amount.Amount;
import se.kth.lwr.jeat.core.*;
import se.kth.lwr.jeat.utils.AmountConverter;
import se.kth.lwr.jeat.utils.DefaultAmountConverter;
import se.kth.lwr.jeat.utils.ProductionSupport;
import se.kth.lwr.jeat.samples.onsitetreatment.SimpleProduction;

/**
 *
 * @author davidev
 */
public class StainlessSteelProducer extends SimpleProduction {

    /**
     *
     * Resource consumtion during production of stainless steel
     *
     */
    public static final InventoryDataSource stainLessSteelInventoryDataSource = new InventoryDataSource("StainLessSteelProductionEmissions",
            "Resource consumtion during production of stainless steel",
            new Inventoriable() {

                @Override
                public Inventory getInventory() {
                    Inventory retval = Inventory.emptyInventory();
                    retval.addEnergyUse(EnergyCategory.ELECTRICAL, Amount.valueOf(15.36, Unit.valueOf("MJ/kg")));
                    retval.addEnergyUse(EnergyCategory.FOSSIL, Amount.valueOf(33.73, Unit.valueOf("MJ/kg")));
                    return retval;
                }
            });
    
    
    /**
     * Density of stainless steel
     * <p/>
     * Value: 8
     * <p/>
     * Unit: kg/L
     */
    public static final AmountDataSource densitySteel = new AmountDataSource<>(
            "densitySteel",
            "Density of stainless steel",
            Amount.valueOf(8, (Unit<VolumetricDensity>) SI.KILOGRAM.divide(NonSI.LITER)));

    private StainlessSteelProducer(ProductionSupport support) {
        super(support);
    }

    public static StainlessSteelProducer getInstance() {
        AmountConverter steelAmontMan=new SteelAmountManager(SI.KILOGRAM);
        ProductionSupport support = new ProductionSupport("Stainless steel",
                stainLessSteelInventoryDataSource, 
                steelAmontMan);
        return new StainlessSteelProducer(support);
    }

    static class SteelAmountManager extends DefaultAmountConverter {

        public SteelAmountManager(Unit acceptable) {
            super(acceptable);
        }

        @Override
        public AmountFunction toCompatibleAmount(AmountDataSource amount) {
            if (amount.readValue().getUnit().isCompatible(SI.CUBIC_METRE)) {
                return new AmountFunction().multiply(amount).multiply(densitySteel);

            } else {
                return super.toCompatibleAmount(amount);
            }
        }
    }
}
